package com.yanqu.road.server.manger.config.grandchild.talent.trigger;

import com.yanqu.road.entity.config.grandchild.GrandChildTalentInfo;
import com.yanqu.road.entity.config.skill.SkillInfo;
import com.yanqu.road.server.manger.config.grandchild.GrandChildConfigMgr;
import com.yanqu.road.server.manger.config.grandchild.talent.entity.TalentTriggerParam;
import com.yanqu.road.server.manger.config.grandchild.talent.entity.TalentTriggerResult;
import com.yanqu.road.server.manger.config.grandchild.talent.entity.TalentTriggerResultBuilder;
import com.yanqu.road.server.manger.config.grandchild.talent.entity.TalentTriggerSkillParam;
import com.yanqu.road.server.manger.config.grandchild.talent.entity.TalentTriggerSkillParamBuilder;
import com.yanqu.road.server.manger.config.grandchild.talent.entity.TalentTriggerSkillResult;
import com.yanqu.road.server.manger.config.grandchild.talent.enums.eGrandChildSkillEffect;
import com.yanqu.road.server.manger.config.grandchild.talent.enums.eGrandChildSkillEffectStage;
import com.yanqu.road.server.manger.config.grandchild.talent.utils.TalentUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class TalentTriggerTQN implements ITalentTrigger {

    @Override
    public TalentTriggerResult getResult(TalentTriggerParam param) {
        List<TalentTriggerSkillResult> skillResultList = new ArrayList<>();

        List<Integer> talentList;
        if (param.getStage() == eGrandChildSkillEffectStage.CHOICE_TALENT) {
            talentList = param.getChosenTalentList();
        } else {
            talentList = param.getGrandChild().getTalentList();
        }

        boolean canTrigger = true;
        Set<Integer> talentQua = new HashSet<>();
        for (int talentId : talentList) {
            GrandChildTalentInfo talentInfo = GrandChildConfigMgr.getGrandChildTalentInfo(talentId);
            if (!talentQua.add(talentInfo.getQuality())) {
                canTrigger = false;
                break;
            }
        }
        if (canTrigger) {

            // 技能生效
            List<SkillInfo> skillInfo = TalentUtils.getSkillInfo(param.getTalentInfo().getSkills(), param.getStage());
            for (SkillInfo info : skillInfo) {
                TalentTriggerSkillParam skillParam = TalentTriggerSkillParamBuilder.aTalentTriggerSkillParam()
                        .setGrandChild(param.getGrandChild())
                        .setSkillInfo(info)
                        .setLastTriggerList(param.getLastTriggerList())
                        .build();
                TalentTriggerSkillResult result = eGrandChildSkillEffect.getResult(skillParam);
                skillResultList.add(result);
            }

        }

        return TalentTriggerResultBuilder.aTalentTriggerResult()
                .setTalentId(param.getTalentInfo().getId())
                .setSkillResultList(skillResultList)
                .build();
    }

}
